const defaultScreenSetting = [
  {
    key: '%CN',
    text: '通道名称',
    previewText: ''
  },
  {
    key: '%TLCW',
    text: '总剩余车位',
    previewText: '1000'
  },
  {
    key: '%TVTCW',
    text: '临时剩余车位',
    previewText: '500'
  },
  {
    key: '%T',
    text: '时间',
    previewText: ''
  }
];

const numberToCN = {
  1: '一',
  2: '二',
  3: '三',
  4: '四'
};
import common from '@/mixins/common.js';
export default {
  props: ['oldChannelList'],
  minxins: [common],
  data() {
    return {
      userScreenText: '',
      defaultScreenSetting,
      numberToCN,
      curEditScreenItem: null,
      curEditScreenIndex: null,
      curEditScreenList: null,
      enterCount: 1,
      leaveCount: 1,
      dialogVisible: false,
      dialogVisibleForThirdLine: false, //LED图文显示(第三行编辑弹框)
      curUploadQrCodeParams: {
        qrcodeOrImg: '1', //(1:显示通道二维码;2:显示图片)
        curItemType: '', //(1:入口;2:出口)
        curItemIndex: '',
        qrcodePicUrl: '', //二维码图片地址
      },
      channelList: {
        enter: [
          {
            uiOpt: {
              isShow: false,
              curTab: '语音播报',
              tabData: ['语音播报', '更多设置'],
              voiceOpenPeriod: ['00:00', '23:59'],
              displayContentArray: [
                [
                  {
                    key: '%CN',
                    text: '通道名称',
                    previewText: ''
                  }
                ],
                [
                  {
                    key: '%T',
                    text: '时间',
                    previewText: ''
                  }
                ],
                [],
                []
              ],
              screenSettingType: '1', //屏显设置类型 (默认设置/LED图文显示)
            },
            qrcodePicUrl: '', //二维码图片地址
            actionType: 0,
            areaID: 0,
            blacklistPassMode: 1,
            boxId: 0,
            boxStatus: 0,
            customCode: '',
            deviceNetModel: 0,
            deviceVolume: 50,
            displayContent: {
              displayContentArray: [['%CN'], ['%T'], [], []]
            },
            displayViewModel: 0,
            dynamicVipToNormalMode: 2,
            enable: true,
            entranceName: '',
            entranceType: 1,
            id: 0,
            isRootChannel: false,
            isSelfOpenGate: 0,
            localVipCarPassMode: 2,
            noChannelMode: 2,
            noPlatePassMode: 3,
            normalCarPassMode: 2,
            parkCode: '',
            parkGlobalID: '',
            parkName: '',
            qrCodeStr: '',
            qrCodeValue: '',
            redListPassMode: 2,
            settlementType: 0,
            shareListPassMode: 0,
            thirdPartyVipCarPassMode: 0,
            timeArray: ['00:00', '23:59'],
            vipNotInPassMode: 2,
            visitorListPassMode: 1,
            voiceOpenPeriod: '',
            ytjSeq: 0,
            ytjStatus: 0
          }
        ],
        leave: [
          {
            uiOpt: {
              isShow: false,
              curTab: '语音播报',
              tabData: ['语音播报', '更多设置'],
              voiceOpenPeriod: ['00:00', '23:59'],
              displayContentArray: [
                [
                  {
                    key: '%CN',
                    text: '通道名称',
                    previewText: ''
                  }
                ],
                [
                  {
                    key: '%T',
                    text: '时间',
                    previewText: ''
                  }
                ],
                [],
                []
              ],
              screenSettingType: '1', //屏显设置类型 (默认设置/LED图文显示)
            },
            qrcodePicUrl: '', //二维码图片地址
            actionType: 0,
            areaID: 0,
            blacklistPassMode: 1,
            boxId: 0,
            boxStatus: 0,
            customCode: '',
            deviceNetModel: 0,
            deviceVolume: 50,
            displayContent: {
              displayContentArray: [['%CN'], ['%T'], [], []]
            },
            displayViewModel: 0,
            dynamicVipToNormalMode: 2,
            enable: true,
            entranceName: '',
            entranceType: 2,
            id: 0,
            isRootChannel: false,
            isSelfOpenGate: 0,
            localVipCarPassMode: 2,
            noChannelMode: 2,
            noPlatePassMode: 3,
            normalCarPassMode: 2,
            parkCode: '',
            parkGlobalID: '',
            parkName: '',
            qrCodeStr: '',
            qrCodeValue: '',
            redListPassMode: 2,
            settlementType: 0,
            shareListPassMode: 0,
            thirdPartyVipCarPassMode: 0,
            timeArray: ['00:00', '23:59'],
            vipNotInPassMode: 2,
            visitorListPassMode: 1,
            voiceOpenPeriod: '',
            ytjSeq: 0,
            ytjStatus: 0
          }
        ]
      }
    };
  },
  created() {
    if (this.oldChannelList) {
      this.channelList = this.oldChannelList;
    }
  },
  methods: {
    checkChannel(rule, value, callback) {
      if (value === '') {
        callback(new Error('通道名称不能为空'));
      }
      let allChannels = this.channelList.enter.concat(this.channelList.leave);
      let nameList = [];
      allChannels.forEach(item => {
        if (item.entranceName) {
          nameList.push(item.entranceName);
        }
      });

      if (nameList.indexOf(value) !== nameList.lastIndexOf(value)) {
        callback(new Error('通道名称重复'));
        return;
      }
      callback();
    },
    enterListChange(val) {
      if (val > this.channelList.enter.length) {
        for (let i = 0, len = val - this.channelList.enter.length; i < len; i++) {
          this.channelList.enter.push({
            uiOpt: {
              isShow: false,
              curTab: '语音播报',
              tabData: ['语音播报', '更多设置'],
              voiceOpenPeriod: ['00:00', '23:59'],
              displayContentArray: [
                [
                  {
                    key: '%CN',
                    text: '通道名称',
                    previewText: ''
                  }
                ],
                [
                  {
                    key: '%T',
                    text: '时间',
                    previewText: ''
                  }
                ],
                [],
                []
              ],
              screenSettingType: '1'
            },
            qrcodePicUrl: '',
            actionType: 0,
            areaID: 0,
            blacklistPassMode: 1,
            boxId: 0,
            boxStatus: 0,
            customCode: '',
            deviceNetModel: 0,
            deviceVolume: 50,
            displayContent: {
              displayContentArray: [['%CN'], ['%T'], [], []]
            },
            displayViewModel: 0,
            dynamicVipToNormalMode: 2,
            enable: true,
            entranceName: '',
            entranceType: 1,
            id: 0,
            isRootChannel: false,
            isSelfOpenGate: 0,
            localVipCarPassMode: 2,
            noChannelMode: 2,
            noPlatePassMode: 3,
            normalCarPassMode: 2,
            parkCode: '',
            parkGlobalID: '',
            parkName: '',
            qrCodeStr: '',
            qrCodeValue: '',
            redListPassMode: 2,
            settlementType: 0,
            shareListPassMode: 0,
            thirdPartyVipCarPassMode: 0,
            timeArray: ['00:00', '23:59'],
            vipNotInPassMode: 2,
            visitorListPassMode: 1,
            voiceOpenPeriod: '',
            ytjSeq: 0,
            ytjStatus: 0
          });
        }
      }
      if (this.channelList.enter.length > val) {
        this.channelList.enter.splice(-(this.channelList.enter.length - val), this.channelList.enter.length - val);
      }
    },
    leaveListChange(val) {
      if (val > this.channelList.leave.length) {
        for (let i = 0, len = val - this.channelList.leave.length; i < len; i++) {
          this.channelList.leave.push({
            uiOpt: {
              isShow: false,
              curTab: '语音播报',
              tabData: ['语音播报', '更多设置'],
              voiceOpenPeriod: ['00:00', '23:59'],
              displayContentArray: [
                [
                  {
                    key: '%CN',
                    text: '通道名称',
                    previewText: ''
                  }
                ],
                [
                  {
                    key: '%T',
                    text: '时间',
                    previewText: ''
                  }
                ],
                [],
                []
              ],
              screenSettingType: '1',
            },
            qrcodePicUrl: '',
            actionType: 0,
            areaID: 0,
            blacklistPassMode: 1,
            boxId: 0,
            boxStatus: 0,
            customCode: '',
            deviceNetModel: 0,
            deviceVolume: 50,
            displayContent: {
              displayContentArray: [['%CN'], ['%T'], [], []]
            },
            displayViewModel: 0,
            dynamicVipToNormalMode: 2,
            enable: true,
            entranceName: '',
            entranceType: 2,
            id: 0,
            isRootChannel: false,
            isSelfOpenGate: 0,
            localVipCarPassMode: 2,
            noChannelMode: 2,
            noPlatePassMode: 3,
            normalCarPassMode: 2,
            parkCode: '',
            parkGlobalID: '',
            parkName: '',
            qrCodeStr: '',
            qrCodeValue: '',
            redListPassMode: 2,
            settlementType: 0,
            shareListPassMode: 0,
            thirdPartyVipCarPassMode: 0,
            timeArray: ['00:00', '23:59'],
            vipNotInPassMode: 2,
            visitorListPassMode: 1,
            voiceOpenPeriod: '',
            ytjSeq: 0,
            ytjStatus: 0
          });
        }
      }
      if (this.channelList.leave.length > val) {
        this.channelList.leave.splice(-(this.channelList.leave.length - val), this.channelList.leave.length - val);
      }
    },
    setCurTab(item, tabItem) {
      if (tabItem === '屏显设置') {
        item.uiOpt.displayContentArray.forEach(sitem => {
          sitem.forEach(pitem => {
            if (pitem.text === '通道名称') {
              pitem.previewText = item.entranceName ? item.entranceName : '通道名称';
            }
            if (pitem.text === '时间') {
              const d = new Date();
              pitem.previewText = `${d.getHours()}:${d.getMinutes()}`;
            }
          });
        });
      }
      item.uiOpt.curTab = tabItem;
    },
    editScreenShow(screenItem, channelItem, list, index) {
      const self = this;
      this.userScreenText = '';
      this.curEditScreenItem = JSON.parse(JSON.stringify(screenItem));
      this.curEditScreenList = list;
      this.defaultScreenSetting.forEach(pitem => {
        if (pitem.text === '通道名称') {
          pitem.previewText = channelItem.entranceName ? channelItem.entranceName : '通道名称';
        }
        if (pitem.text === '时间') {
          const d = new Date();
          pitem.previewText = `${d.getHours()}:${d.getMinutes()}`;
        }
      });
      this.curEditScreenIndex = index;
      this.dialogVisible = true;
    },
    editUploadQrCodeShow(type,index){
      this.curUploadQrCodeParams.qrcodeOrImg = '1'
      this.curUploadQrCodeParams.curItemType = type
      this.curUploadQrCodeParams.curItemIndex = index
      this.curUploadQrCodeParams.qrcodePicUrl = ''
      this.dialogVisibleForThirdLine = true
    },
    addTagToScreenShow(item) {
      const isLen = this.curEditScreenItem.filter(sitem => sitem.text === item.text).length;
      if (!isLen) {
        this.curEditScreenItem.push(item);
      } else {
        return;
      }
    },
    addUserScreenText() {
      if (!this.userScreenText) return;
      const userScreenText = {
        key: this.userScreenText,
        previewText: this.userScreenText,
        text: 'userText'
      };
      this.curEditScreenItem.push(userScreenText);
    },
    delTag(index) {
      this.curEditScreenItem.splice(index, 1);
    },
    confirmScreen() {
      this.dialogVisible = false;
      this.curEditScreenList.splice(this.curEditScreenIndex, 1, this.curEditScreenItem);
    },
    delChannelItem(list, index) {
      list.splice(index, 1);
    },
    goBackStep() {
      this.$emit('setFormData', 'channelVoList', this.channelList);
      this.$emit('goBackSetp');
    },
    testChannel() {
      this.$refs['channelList'].validate(valid => {
        if (valid) {
          this.$emit('setFormData', 'channelVoList', this.channelList);
          this.$emit('submitForm');
        } else {
        }
      });
    },
  }
};
